/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hd.assistant.web.home.module.screen.share;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.web.common.base.share.BaseTaobaoShareModule;
import com.hd.assistant.web.common.util.ClickedUtil;
import com.taobao.api.domain.Item;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemsOnsaleGetResponse;

/**
*
* @author Hangding
*/
public class ItemList extends BaseTaobaoShareModule {
    private final static Logger log = LoggerFactory.getLogger(ItemList.class);

    @Autowired
    private HttpSession session;
    
    
    public void execute(
    		@Param("pindex") String pindex,
    		@Param("index") String index,
    		@Param("type") String type, 
    		@Param("page") Long page, 
    		@Param("pageSize") Long pageSize, 
    		@Param("query") String queryStr,
    		@Param("orderby") String orderby, 
    		Context context,Navigator nav) {
    	try{
    		context.put("pindex", pindex==null?1:pindex);
        	context.put("index", index==null?100:index);
        	context.put("type",type);
        	context.put("query",queryStr);
        	context.put("orderby",orderby);
        	
       
        	User user = checkPermision(User.Type_TAOBAO_SHARE,session,context,nav);
        	ItemsOnsaleGetRequest request = new ItemsOnsaleGetRequest();
        	if(pageSize == null)
        		pageSize = 10l;
        	if(page == null)
        		page = 1l;
        	if(queryStr != null)
        		request.setQ(queryStr);
        	if(orderby != null)
        		request.setOrderBy(orderby);
        	if(page != null)
        		request.setPageNo(page);
        	if(pageSize != null)
        		request.setPageSize(pageSize);
        	
        	ItemsOnsaleGetResponse response = this.taobaoISVAppManager.getOnSaleItemsByRequest(request, this.taobaoISVAppManager.getShareAppKey(), 
        			 this.taobaoISVAppManager.getShareAppSecret(), user.getToken());
        	List<Item> items = response.getItems();
        	generateClickedId(items);
        	Long pageTotal = response.getTotalResults() /pageSize;
        	if(response.getTotalResults() % pageSize != 0)
        		pageTotal = pageTotal+1;
        	long nextPage = page+1 > pageTotal ? pageTotal : page+1;
        	long prePage = page-1 < 1 ? 1 : page-1;
        	context.put("result", response);
        	context.put("pageTotal", pageTotal);
        	context.put("nextPage", nextPage);
        	context.put("prePage", prePage);
        	context.put("page", page);
        	context.put("pageSize", pageSize);
    	}catch(Exception e){
    		log.error("error:",e);
    	}
        	

    }

	private void generateClickedId(List<Item> items) {
		for(Item item :items){
			String clickId = ClickedUtil.getClickedId(ClickedUtil.TYPE_TAOBAO, item.getNumIid()+"");
			item.setClickId(clickId);
		}
	}
    
   
}
